﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using CSLauncher.Commands;
using CSLauncher.Configuration.Interface;
using CSLauncher.Configuration.Xml;

namespace CSLauncher
{
    internal partial class CSForm : Form
    {
        [DllImport("user32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);

        private static readonly Dictionary<char, int> charMap = new Dictionary<char, int>()
        {
            {'1', 1},                                                            
            {'2', 2},
            {'3', 3},
            {'4', 4},
            {'5', 5},
            {'6', 6},
            {'7', 7},
            {'8', 8},
            {'9', 9},
            {'0', 0},
        };

        private readonly Button[] buttonsArray;

        private readonly ICSLauncherConfig configuration;
        private ISubmenu currentMenu;

        public CSForm()
        {
            InitializeComponent();
            buttonsArray = new[] {
                exitButton,
                button1,
                button2,
                button3,
                button4,
                button5,
                button6,
                button7,
                button8,
                button9
            };
            configuration = XmlConfiguration.GetDefault();
            currentMenu = configuration.MainMenu;
            UpdateGUI();
            Opacity = Properties.Settings.Default.MenuOpacity;
        }

        private void HandleClick(int number)
        {
            if (0 == number)
            {
                if (currentMenu.Parent == null)
                {
                    Close();
                }
                else
                {
                    currentMenu = currentMenu.Parent;
                    UpdateGUI();
                }
            }    
            
            IItem item = currentMenu.Items[number];

            ISubmenu submenu = item as ISubmenu;
            if (submenu != null)
            {
                currentMenu = submenu;
                UpdateGUI();
                return;
            }

            ICommand command = item as ICommand;
            if (command != null)
            {
                try
                {
                    command.Launch();    
                }
                catch (Exception e)
                {
                    MessageBox.Show(string.Format("Failed executing command: {0}", e.Message), "CSLauncher", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                
                Close();
                return;
            }
        }

        private void UpdateGUI()
        {
            int i;
            
            for (i = 1; i < currentMenu.Items.Count; ++i)
            {
                IItem item = currentMenu.Items[i];

                buttonsArray[i].Text = string.Format("{0}. {1}", i, item.Name);
                buttonsArray[i].Visible = true;
                buttonsArray[i].Enabled = true;
            }

            for (; i < buttonsArray.Length; ++i)
            {
                buttonsArray[i].Visible = false;
                buttonsArray[i].Enabled = false;
            }
        }

        private void ButtonClick(object sender, EventArgs e)
        {
            Button clickedButton = (Button)sender;
            HandleClick((int)clickedButton.Tag);
        }

        private void CSFormKeyPress(object sender, KeyPressEventArgs e)
        {
            if (charMap.ContainsKey(e.KeyChar))
            {
                HandleClick(charMap[e.KeyChar]);
            }
        }

        private void CSFormLoad(object sender, EventArgs e)
        {
            SetForegroundWindow(Handle);
        }
    }
}
